﻿// -----------------------------------------------------------------------
// <copyright file="ShoppingList.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using WSU.PMP.BE.DATA.EF;

namespace WSU.PMP.BE.DATA.Concrete
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using WSU.PMP.BE.DATA.Abstract;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ShoppingList : IShoppingList
    {
        public EfShoppingList getShoppingListById(string ShoppingListId)
        {
            using (var db = new PmpEntities())
            {
                long key = long.Parse(ShoppingListId);

                EfShoppingList shoppingList = (from i in db.EfShoppingLists
                                         where i.ShoppingListID == key
                                         select i).FirstOrDefault();

                return shoppingList;
            }
        }

        public EfShoppingList getShoppingListByName(string name)
        {
            using (var db = new PmpEntities())
            {
                EfShoppingList shoppingList = (from i in db.EfShoppingLists
                                         where i.Name == name
                                         select i).FirstOrDefault();

                return shoppingList;
            }
        }

        public List<EfShoppingList> getShoppingListsByName(string name)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfShoppingList> shoppingLists = from i in db.EfShoppingLists
                                                           where i.Name.Contains(name)
                                                           select i;

                return shoppingLists.ToList();
            }
        }

        public List<EfShoppingList> getShoppingListsByDate(DateTime date)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfShoppingList> shoppingLists = from i in db.EfShoppingLists
                                                           where i.Date.ToShortDateString() == date.ToShortDateString()
                                                           select i;

                return shoppingLists.ToList();
            }
        }

        public List<EfShoppingList> getShoppingListsByDateRange(DateTime start, DateTime end)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfShoppingList> shoppingLists = from i in db.EfShoppingLists
                                                           where i.Date >= start && i.Date < end
                                                           select i;

                return shoppingLists.ToList();
            }
        }

        public List<EfShoppingList> getShoppingListsByItem(string barcode)
        {
            using (var db = new PmpEntities())
            {
                long key = long.Parse(barcode);

                IQueryable<EfShoppingList> shoppingLists = from s in db.EfShoppingLists
                                                           join si in db.EfShoppingListItems on s.ShoppingListID equals si.ShoppingListID
                                                           join i in db.EfItems on si.Item.Barcode equals i.Barcode
                                                           where i.Barcode == key
                                                           select s;

                return shoppingLists.ToList();
            }
        }

        public List<EfShoppingList> getShoppingListsByItemName(string name)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfShoppingList> shoppingLists = from s in db.EfShoppingLists
                                                           join si in db.EfShoppingListItems on s.ShoppingListID equals si.ShoppingListID
                                                           join i in db.EfItems on si.Item.Barcode equals i.Barcode
                                                           where i.Name.Contains(name)
                                                           select s;

                return shoppingLists.ToList();
            }
        }

        public void addShoppingList(EfShoppingList list)
        {
            using (var db = new PmpEntities())
            {
                db.EfShoppingLists.AddObject(list);
                db.SaveChanges();
            }
        }

        public void editShoppingList(EfShoppingList list)
        {
            using (var db = new PmpEntities())
            {
                db.EfShoppingLists.Attach(db.EfShoppingLists.Single(i => i.ShoppingListID == list.ShoppingListID));
                db.EfShoppingLists.ApplyCurrentValues(list);
                db.SaveChanges();
            }
        }

        public void deleteShoppingList(EfShoppingList list)
        {
            using (var db = new PmpEntities())
            {
                db.EfShoppingLists.Attach(list);
                db.EfShoppingLists.DeleteObject(list);
                db.SaveChanges();
            }
        }
    }
}
